<?php   
namespace  Admin\Controller;
use  Think\Controller;

class  ServerController  extends  AdminController{
	/*服务器列表*/
	public function index(){
		      // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%' . $keyword . '%');
        $map['server_name|id|customer_id'] = array($condition,$condition,$condition,'_multi' => true );
 
        // 获取所有用户
        $data_list = D('Server')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();   
        $page = new \Common\Util\Page(D('Server')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
        
        foreach ($data_list as $key => &$val) {
        	$customerid=$val['customer_id'];
            $val['customer_id']=get_customer_info($val['customer_id'],'customer_name');
			$val['customer_id']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer_id']."</a>";                       $tt['tablename']='server_system';			$tt['fieldvalue']=$val['system'];			$server_system=D('Descriptions')->where($tt)->find();			$val['system']=$server_system['title'];
            $tt1['tablename']='server_buyer';			
            $tt1['fieldvalue']=$val['supplier'];			
            $server_buyer=D('Descriptions')->where($tt1)->find();			
            $val['supplier']=$server_buyer['title'];						
            switch($val['place']){
            	case "1":				
            		$val['place']="香港";				
            		break;				
            		case "2":				
            		$val['place']="美国";				
            		break;				
            		case "3":				
            		$val['place']="大陆";				
            		break;			
			}               
		}
     
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('项目信息列表')
             ->addTopButton('addnew') // 设置页面标题
            ->addTopButton('delete') // 添加新增按钮

            ->setSearch('请输入服务器名称/id', U('index'), $project_status) // 添加删除按钮

            ->addTableColumn('server_name', '服务器名称')
            ->addTableColumn('customer_id', '客户')
            ->addTableColumn('s_time', '开通时间','time')
            ->addTableColumn('e_time', '到期时间','time')
            ->addTableColumn('ip', '服务器IP')
            ->addTableColumn('account', '管理账号')
           
			->addTableColumn('system', '操作系统')
//          ->addTableColumn('system', '操作系统')			
//          ->addTableColumn('ctime', '创建时间','time')
//			->addTableColumn('status', '状态')
//          ->addTableColumn('remark', '备注')
 			->addTableColumn('supplier', '供应商')               
 			->addTableColumn('place', '位置')
            
			
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)
            ->setTableDataPage($page->show())
            ->addRightButton('edit')
          

//            ->addRightButton('self', array('title' => '添&nbsp;任','class' => 'label label-rw','href' => U('Admin/task/add', array('projectid' => '__data_id__'))))

            ->display();
	}
/*新增服务器*/
		public  function  add(){
			$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
			$sys=D('Descriptions')->where('tablename="server_system"')->field('fieldvalue as id,title')->select();
			if(IS_POST){
			    $_POST['s_time']=strtotime($_POST['s_time']);
				$_POST['e_time']=strtotime($_POST['e_time']);
				$_POST['c_time']=strtotime($_POST['c_time']);				
				$_POST['c_user']=is_login();
				$server=D('Server');
				$data=$server->create();				
				if($data){
					$id=$server->add();
					if($id){
						$this->success('新增成功',U('index'));
					}else{
						$this->error('新增失败');
					}
				}else{
					$this->error($server->getError());
				}
			}else{
				$id=I('id');			
				if(!empty($id)){
									$map['id']=$id;			
				}				
				$cus=M("Customer")->field("id,customer_name")->select();				
				foreach($cus as $k=>$v){
										$cuss[$v['id']]=$v['customer_name'];				
				}                                  
				$place=array("1"=>"香港","2"=>"美国","3"=>"大陆");                 
				$bulider=new \Common\Builder\FormBuilder();
				$bulider->setMetaTitle('添加服务器')//设置页面标题
				        ->setPostUrl(U('add'))//表单提交地址
						->addFormItem('server_name','text','服务器名称','服务器名称')                       
						->addFormItem('ip','text','服务器ip','ip地址')                       
						->addFormItem('place','select','服务器位置','服务器位置',$place)                       
						->addFormItem('system','select2','操作系统','操作系统',$sys)	                                             
						->addFormItem('supplier','select2','供应商','供应商',$supp)
                      // 	->addFormItem('customer_id','select','所属客户','客户id',$cuss) 
						->addFormItem('s_time','date','开通时间','开通时间')
						->addFormItem('e_time','date','过期时间','服务器过期时间')                         
						->addFormItem('price','text','续费价格','续费价格')
						->addFormItem('yprice','text','市场价格','市场价格')
						->addFormItem('customer_id','user_list','所属客户','客户id',$map)
                       
																				
						  ->addFormItem('account','textarea','服务器远程账号','服务器远程账号')                       
						  ->addFormItem('info','textarea','配置信息','配置信息')
						->addFormItem('remark','textarea','备注','备注')
						 
						->display();
						 
			}
		}
 /**
  *编辑 
  */
		public  function edit($id){		$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();		$sys=D('Descriptions')->where('tablename="server_system"')->field('fieldvalue as id,title')->select();
			$info=M('Server')->find($id);
			if(IS_POST){
				$server=D('Server');
			  $_POST['s_time']=strtotime($_POST['s_time']);				$_POST['e_time']=strtotime($_POST['e_time']);
				$data=$server->create();
				if($data){
					$res=$server->save();
					if($res){
						$this->success('更新成功',U('index'));
					}else{
						$this->error('更新失败');
					}
				}else{
					$this->error($server->getError());
				}
			}else{							  $place=array("1"=>"香港","2"=>"美国","3"=>"大陆");								$cus=M("Customer")->field("id,customer_name")->select();				foreach($cus as $k=>$v){					$cuss[$v['id']]=$v['customer_name'];				}
				$bulider=new \Common\Builder\FormBuilder();
				$bulider->setMetaTitle('编辑服务器')
				        ->setPostUrl(U('edit'))
						->addFormItem('id','hidden','ID','ID')
				       ->addFormItem('server_name','text','服务器名称','服务器名称')                       
				       ->addFormItem('ip','text','服务器ip','ip地址')                       
				       ->addFormItem('place','select','服务器位置','服务器位置',$place)                       
				       ->addFormItem('system','select2','操作系统','操作系统',$sys)	                                             
				       ->addFormItem('supplier','select2','供应商','供应商',$supp)                    						
				       ->addFormItem('customer_id','user_list','所属客户','客户id',$map)
                       ->addFormItem('s_time','date','开通时间','开通时间')						
                       ->addFormItem('e_time','date','过期时间','服务器过期时间')                         
                       ->addFormItem('price','text','续费价格','续费价格')
					   ->addFormItem('yprice','text','市场价格','市场价格')                               
                       ->addFormItem('account','textarea','服务器远程账号','服务器远程账号')                       
                       ->addFormItem('info','textarea','配置信息','配置信息')                      
                       ->addFormItem('remark','textarea','备注','备注')						
                       ->setFormData($info)						
                       ->display();					//	->addFormItem('manage_account','text','管理账号','账号')					//	->addFormItem('manage_pwd','text','管理密码','密码')					//	->addFormItem('manage_port','text','管理端口','端口')                   //  ->addFormItem('customer_id','text','所属客户','客户id')					//	->addFormItem('customer_id','select','所属客户','客户id',$cuss)						
			}
		}






















}
